package com.songcha.library_common.ui.view

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.view.marginRight
import androidx.core.view.size
import androidx.fragment.app.Fragment
import com.songcha.library_common.util.dp2px
import com.songcha.library_common.R
import com.songcha.library_common.util.StatusBarUtil



class HeaderBarView : FrameLayout {
    interface IOnHeaderBarGoBackListener{
        fun onGoBack()
    }

    private var mTitle=""
    private var mTvTitle:TextView?=null
    private var mIvBack:ImageView?=null
    private var mllRightBtns:LinearLayout?=null
    private var mOnGoBackListener: IOnHeaderBarGoBackListener? =null
    private var mShowStatusHeight=false
    private var mIsDark=true
    private var mBtns:MutableList<View> = mutableListOf()

    companion object{
        val DEFAULT_HEIGHT=42.dp2px()
    }
    //private var mShowStatusHeight:Boolean=false

    constructor(context: Context) : super(context) {
        initView(context,null)
    }

    constructor(context: Context, attrs: AttributeSet):this(context,attrs,0){

    }

    constructor(context: Context, attrs: AttributeSet, defStyle:Int):this(context,attrs,defStyle,0){

    }

    constructor(context: Context, attrs: AttributeSet, defStyle:Int, defRes:Int):super(context,attrs,defStyle,defRes){
        initView(context,attrs)
    }

    private fun initView(context: Context, attrs: AttributeSet?){
        if(attrs!=null){
            val ta=context.obtainStyledAttributes(attrs, R.styleable.HeaderBarView)
            mTitle=ta.getString(R.styleable.HeaderBarView_title)?:""
            mShowStatusHeight=ta.getBoolean(R.styleable.HeaderBarView_show_status_height,false)
            mIsDark=ta.getBoolean(R.styleable.HeaderBarView_headerbar_is_dark,true)
            ta.recycle()
        }

        val view=LayoutInflater.from(context).inflate(R.layout.common_view_header_bar,this,false)

        mTvTitle=view.findViewById(R.id.header_bar_tv_title)
        mTvTitle!!.text=mTitle
        mIvBack=view.findViewById(R.id.iv_back)
        mIvBack!!.setOnClickListener {
            if(mOnGoBackListener==null && context is Activity){
                context.finish()
            }else{
                mOnGoBackListener!!.onGoBack()
            }

        }
        if(!mIsDark){
            mTvTitle!!.setTextColor(Color.WHITE)
            mIvBack!!.setImageResource(R.mipmap.common_icon_back_white)
        }

        mllRightBtns=view.findViewById(R.id.header_right_btns_ll)

        addView(view,ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT)


        if(mShowStatusHeight){
            val statusBar=view.findViewById<View>(R.id.hearder_bar_status)
            statusBar.visibility=View.VISIBLE
            statusBar.layoutParams=statusBar.layoutParams.apply {
                if(context is Activity) {
                    height = StatusBarUtil.getStatusBarHeight(context)
                }else if(context is Fragment){
                    height = StatusBarUtil.getStatusBarHeight(context.requireActivity())
                }
            }
        }


    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        var hms=heightMeasureSpec
        val heightMode=MeasureSpec.getMode(heightMeasureSpec)
        if(heightMode==MeasureSpec.AT_MOST || heightMode==MeasureSpec.UNSPECIFIED){
            var h=DEFAULT_HEIGHT
            if(mShowStatusHeight){
                if(context is Activity) {
                    h += StatusBarUtil.getStatusBarHeight(context as Activity)
                }else if(context is Fragment){
                    h += StatusBarUtil.getStatusBarHeight((context as Fragment).requireActivity())
                }
                //h+=StatusBarUtil.getStatusBarHeight(context)
            }
            hms=MeasureSpec.makeMeasureSpec(h,MeasureSpec.EXACTLY)
        }
        super.onMeasure(widthMeasureSpec, hms)
    }

    fun setTitle(title:String){
        mTitle=title
        mTvTitle?.text=mTitle
    }

    fun setTitleColor(color:Int){
        mTvTitle?.setTextColor(color)
    }

    fun setOnGoBackListener(listener: IOnHeaderBarGoBackListener?){
        mOnGoBackListener=listener
    }

    fun addTextButton(text:String,textSize:Float,textColor:Int,marginRight:Int,onClickListener: OnClickListener){
        if(mllRightBtns==null) return
        val tv = TextView(context)
        tv.text = text
        tv.textSize = textSize
        tv.setTextColor(textColor)
        val lp = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT)
        lp.rightMargin = marginRight
        tv.layoutParams = lp
        mllRightBtns!!.visibility = View.VISIBLE
        mllRightBtns!!.addView(tv)
        tv.setOnClickListener(onClickListener)
        mBtns.add(tv)
    }

    fun addImageButton(resId:Int,size:Int,marginRight:Int,onClickListener: OnClickListener){
        if(mllRightBtns==null) return
        val iv = ImageView(context)
        iv.setImageResource(resId)
        iv.scaleType = ImageView.ScaleType.CENTER_CROP
        val lp = LinearLayout.LayoutParams(size,size)
        lp.rightMargin = marginRight
        iv.layoutParams = lp
        mllRightBtns!!.visibility = View.VISIBLE
        mllRightBtns!!.addView(iv)
        iv.setOnClickListener(onClickListener)
        mBtns.add(iv)
    }

    fun getButton(index:Int):View?{
        if(index>mBtns.size-1) return null
        return mBtns[index]
    }
}